package org.q.job;

import java.util.ArrayList;
import java.util.List;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class MainThread {
	
	public static void main(String[] args) throws SchedulerException{
		System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true");
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();  
        Scheduler scheduler = schedulerFactory.getScheduler();
        List<String> rules = new ArrayList<String>();
        rules.add("rule1");
        rules.add("rule2");
        for(int i=0;i<rules.size();i++) {
        	JobDetail job = JobBuilder.newJob(CmdProcessor.class)
                    .withIdentity("rule"+(i+1), "rule")
                    .build();
            Trigger simpleTrigger = TriggerBuilder.newTrigger()
                    .withIdentity("rule-"+(i+1)+"_trigger", "rule")
                    //quartz的bug， startNow将会在scheduleJob时以当前时间作为启动时间， 而任务多了以后， 轮到此任务时， 时间已经超过startNow所设置的启动时间， 所以第一次不会执行
//                    .startAt(DateBuilder.nextGivenSecondDate(null, 10))
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withMisfireHandlingInstructionFireNow()
                            .repeatForever())
                    .build();
            scheduler.scheduleJob(job, simpleTrigger);
        }
        scheduler.start();
	}

}
